package module.edittroop.view.editnewtrooppanel
{
	import code.analyze.ConfigAnalyze;
	import code.ui.SimpleToolTips;
	import code.utils.MaterialUtil;
	
	import com.darwin.darwinUI.containers.ContainerBase;
	import com.darwin.darwinUI.controls.Button;
	import com.darwin.darwinUI.controls.Label;
	import com.darwin.darwinUI.controls.List;
	import com.darwin.darwinUI.controls.TextInput;
	import com.darwin.darwinUI.core.UIComponent;
	import com.darwin.darwinUI.events.UIEvent;
	import com.darwin.darwinUI.general.TextFieldFactory;
	import com.darwin.darwinUI.manager.ToolTipManager;
	import com.darwin.darwinUI.manager.UIManager;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.filters.GlowFilter;
	import flash.media.Video;
	
	import module.edittroop.event.EditTroopEvent;
	import module.edittroop.model.dataObj.SkillConfigObj;
	import module.edittroop.view.troopinfopanel.HeadFarem;
	import module.main.view.ResourceConst;

	public class EditNewSoldierInfoPanel extends ContainerBase
	{
		/**
		 *点击移页按钮 
		 */		
		private var _smallDistancBtnRight:Button;
		private var _smallDistancBtnLeft:Button;
		private var uiComponent:UIComponent;
		private var _list:List;
		
		private var _skillUp:Sprite;
		private var _skillDown:Sprite;
		/**
		 * 玩家头像框
		 */		
		private var _headFream:Sprite;
		/**
		 *玩家头像背景面板（小） 
		 */		
		private var _headFreamSmallBg:Sprite
		/**
		 * 玩家头像背景面板（大）
		 */		
		private var _headFreamBigBg:Sprite;
		private var _textInPutBg:Sprite;

		private var _style:uint;
		private var _plusBtn:Button;
		private var _minusBtn:Button;
		private var _maxBtn:Button;
		/////////文本变量
		private var _heroNameLabel:Label;
		private var _heroPropertyLabel:Label;
		private var _fiveLinesLabel:Label;
		
		///////兵种属性文本
		/**
		 *兵种文本变量 
		 * 
		 */		
		private var _armsLabel:Label;
		private var _branchesLabel:Label;
		/**
		 *兵力文本 
		 */		
		private var _troopsLabel:Label;
		private var _troopsNumLable:Label;
		/**
		 *技能文本 
		 */		
		private var _skillLabel:Label;
		private var _skillNameLabel:Label;
		/**
		 * 
		 * @param styel  表示不同的布局默认为0
		 * 
		 */		
		public function EditNewSoldierInfoPanel(styel:uint = 0)
		{
			_style = styel;
			_heroNameLabel = TextFieldFactory.getInstance().getLabel("building_tips_barrack_description");;
			_heroPropertyLabel = TextFieldFactory.getInstance().getLabel("building_tips_barrack_description");;
			_fiveLinesLabel = TextFieldFactory.getInstance().getLabel("building_tips_barrack_description");;
			uiComponent = new UIComponent();
			_smallDistancBtnRight = new Button();
			_smallDistancBtnLeft = new Button();
			_plusBtn = new Button;
			_minusBtn = new Button;
			_maxBtn = new Button;
			_textInTo = new TextInput;
			addItem(uiComponent);
			initView();
		}

		public function set troopNum(value:uint):void
		{
			_troopNum = value;
			var toopNumEvent:EditTroopEvent = new EditTroopEvent(EditTroopEvent.ADD_TROOP_NUM);
			var obj:Object = new Object();
			obj.troopNum = _troopNum;
			toopNumEvent.data = obj;
			dispatchEvent(toopNumEvent);
		}

		public function get troopNum():uint
		{
			return _troopNum;
		}

		public function set heroInfo(value:Object):void
		{
			_heroInfo = value;
		}

//		public function set list(value:List):void
//		{
//			_list = value;
//		}

		public function get list():List
		{
			return _list;
		}

		private function initView():void
		{
			_textInTo.addEventListener(TextEvent.TEXT_INPUT,inputHandel);
			
			_smallDistancBtnRight.background = MaterialUtil.getPreloadMaterial("EditTroop",ResourceConst.EDIT_TROOP_SIGLE_BUTTON) as Sprite;
			_smallDistancBtnLeft.background = MaterialUtil.getPreloadMaterial("EditTroop",ResourceConst.EDIT_TROOP_TOTLE_MOVE_BUTTON_LEFT) as Sprite;
			var sprite:HeadFarem = new HeadFarem();
			_headFreamSmallBg = MaterialUtil.getPreloadMaterial("EditTroop",ResourceConst.EDIT_NEW_TROOP_FREAM_BG) as Sprite;
			_headFreamBigBg = MaterialUtil.getPreloadMaterial("EditTroop",ResourceConst.EDIT_NEW_TROOP_BG) as Sprite;
			_headFream = MaterialUtil.getPreloadMaterial("EditTroop",ResourceConst.EDIT_TROOP_BIG_HEAD_FRAME) as Sprite;
			_textInPutBg = MaterialUtil.getPreloadMaterial("EditTroop",ResourceConst.EDIT_NEW_TROOP_SOLDIER_NUM) as Sprite;
			
			_plusBtn.backgroundSkin = "EditTroop." + ResourceConst.EDIT_NEW_TROOP_PLUS;
			_plusBtn.backgroundAuto = true;
			
			_minusBtn.backgroundSkin = "EditTroop." + ResourceConst.EDIT_NEW_TROOP_MINUS;
			_minusBtn.backgroundAuto = true;
			
			_maxBtn.backgroundSkin = "EditTroop." + ResourceConst.EDIT_NEW_TROOP_MAX;
			_maxBtn.backgroundAuto = true;
			
			_skillUp = MaterialUtil.getPreloadMaterial("EditTroop",ResourceConst.SKILL_ICON_UP) as Sprite;
			_skillDown = MaterialUtil.getPreloadMaterial("EditTroop",ResourceConst.SKILL_ICON_DOWN) as Sprite;
			
			uiComponent.addChild(_headFreamBigBg);
			_smallDistancBtnRight.width = 19.9;
			_smallDistancBtnRight.height = 29.9;
			_smallDistancBtnLeft.width = 19.9;
			_smallDistancBtnLeft.height = 29.9;
			
			_list = new List(HeadFarem,sprite.width,sprite.height,5,5);
			_list.width = sprite.width * 4 + 20;
			_list.height = sprite.height * 2 + 5;
			_list.source = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16];
			//_list.selectedIndex = 8;
			
			this.width = 421.9;
			this.height = 169.0;
			setPosition();
		}
		private function setPosition():void
		{
			setChangObj();
			setHeadFreamSmallBg();
			setList();
			setSmallDistancBtnLight();
			setSmallDistancBtnRight();
		}
		private function setSmallDistancBtnRight():void
		{
			_smallDistancBtnRight.x =_smallDistancBtnLeft.x + _smallDistancBtnLeft.width + _list.width + 5;
			_smallDistancBtnRight.y = _smallDistancBtnLeft.y;
			_headFreamSmallBg.addChild(_smallDistancBtnRight);
		}
		private function setSmallDistancBtnLight():void
		{
			_smallDistancBtnLeft.x =_list.x / 2 - 10;
			_smallDistancBtnLeft.y = _list.y + _list.height / 2 - 14;
			_headFreamSmallBg.addChild(_smallDistancBtnLeft);
		}
		private function setHeadFreamSmallBg():void
		{
			_headFreamSmallBg.x = 124;
			_headFreamSmallBg.y = 10
			_headFreamBigBg.addChild(_headFreamSmallBg);
		}
		private function setList():void
		{
			_list.x =  (_headFreamSmallBg.width - _list.width) / 2 ;
			_list.y = (_headFreamSmallBg.height - _list.height) / 2;
			//_list.addEventListener(UIEvent.LISTITEM_CLICK,listTitemClickHandler);
//			_list.rollToPage(0,1);
			_headFreamSmallBg.addChild(_list);
		}
		private function removeFilter():void
		{
			_headFaremFitel.filters = [];
		}
		private var _headFaremFitel:HeadFarem;
		private function listTitemClickHandler(e:UIEvent):void
		{
			if((e.data as HeadFarem) !=_headFaremFitel&&_headFaremFitel)
			{
				removeFilter();
			}
			_headFaremFitel = e.data as HeadFarem;
			var $fiter:GlowFilter = new GlowFilter();
			$fiter.alpha = 0.5
			var $filtersArray:Array = new Array($fiter); 
			(e.data as HeadFarem).filters = $filtersArray;
			changInfoData((e.data as HeadFarem).data);
		}
		private function setChangObj():void
		{
			if(_style == 0)
			{
				setHeadFream();
				setHeroNameLable();
				setHeroPropertyLabel();
				setFiveLinesLabel();
			}
			if(_style == 1)
			{
				//设置文本位置
				setTxtPosition();
				//设置按钮位置
				setButton();
			}
		}
		private var _textInTo:TextInput;
		private function setButton():void
		{
			_minusBtn.x =13;
			_minusBtn.y = _headFreamSmallBg.y + _headFreamSmallBg.height / 4 * 3 + 10;
			_minusBtn.addEventListener(MouseEvent.CLICK,clickHandler);
			_headFreamBigBg.addChild(_minusBtn);
			
			_textInPutBg.x = _minusBtn.x + 16.9;
			_textInPutBg.y = _minusBtn.y;
			_textInTo.height = 17;
			_textInTo.width = 38;
			_textInTo.text = _troopsNumLable.text;
			_textInTo.y = -3;
			_textInTo.border = false;
			_headFreamBigBg.addChild(_textInPutBg);
			_textInPutBg.addChild(_textInTo);
			
			_plusBtn.x = _textInPutBg.x + _textInPutBg.width;
			_plusBtn.y = _minusBtn.y;
			_headFreamBigBg.addChild(_plusBtn);
			_plusBtn.addEventListener(MouseEvent.CLICK,clickHandler);
			
			_maxBtn.x = _plusBtn.x + 16.9;
			_maxBtn.y = _plusBtn.y;
			_headFreamBigBg.addChild(_maxBtn);
			_maxBtn.addEventListener(MouseEvent.CLICK,clickHandler);
		}
		private function inputHandel(e:TextEvent):void
		{
			troopNum = int(_textInTo.text + "" + e.text);
			trace(_troopNum);
		}
		private  function clickHandler(e:MouseEvent):void
		{
			switch(e.currentTarget)
			{
				case _minusBtn:
					if(_troopNum == 0)
					{
						return;
					}
					else
					{
						troopNum = troopNum - 1;
						_textInTo.text = troopNum.toString();
						_troopsNumLable.text = _textInTo.text;
					}
					break;
				case _plusBtn:
					troopNum = troopNum + 1;
					_textInTo.text = troopNum.toString();
					_troopsNumLable.text = _textInTo.text;
					break;
				case _maxBtn:
					if(_heroInfo)
					{
						troopNum = _heroInfo.numLimit;
						_textInTo.text = troopNum.toString();
						_troopsNumLable.text = _textInTo.text;
					}
					break;
			}
		}
		private var _troopNum:uint;
		private function setTxtPosition():void
		{
			/////////////兵种
			_armsLabel = TextFieldFactory.getInstance().getLabel("building_tips_barrack_description");
			_branchesLabel = TextFieldFactory.getInstance().getLabel("building_tips_barrack_description");
			_armsLabel.text = "兵种  :";
			_armsLabel.x = 15;
			_armsLabel.y = 15;
			_branchesLabel.text = "";
			_branchesLabel.x = _armsLabel.x + _armsLabel.width ;
			_branchesLabel.y = _armsLabel.y;
			_headFreamBigBg.addChild(_armsLabel);
			_headFreamBigBg.addChild(_branchesLabel);
			
			/////////兵力
			_troopsLabel = TextFieldFactory.getInstance().getLabel("formation_arms_troops");
			_troopsNumLable = TextFieldFactory.getInstance().getLabel("building_tips_barrack_description");
			_troopsNumLable.text = "";
			_troopsLabel.text = "兵力  :";
			_troopsLabel.x = 15;
			_troopsLabel.y = _armsLabel.y + _armsLabel.height;
			_troopsNumLable.x = _troopsLabel.x + _troopsLabel.width ;
			_troopsNumLable.y = _troopsLabel.y;
			_headFreamBigBg.addChild(_troopsLabel);
			_headFreamBigBg.addChild(_troopsNumLable);
			
			///////兵种技能
			_skillLabel = TextFieldFactory.getInstance().getLabel("building_tips_barrack_description");
			_skillNameLabel = TextFieldFactory.getInstance().getLabel("building_tips_barrack_description");
			_skillNameLabel.text = "";
			_skillLabel.text = "兵种技能 :";
			_skillLabel.x = 15;
			_skillLabel.y = _troopsLabel.y + _skillLabel.height;
			_skillNameLabel.x = _skillLabel.x + _skillLabel.width;
			_skillNameLabel.y = _skillLabel.y;
			_headFreamBigBg.addChild(_skillLabel);
			_headFreamBigBg.addChild(_skillNameLabel);
		}
		private function setHeadFream():void
		{
			_skillUp.x = _headFream.width - _skillUp.width / 2;
			_skillUp.y = (_headFream.height / 2 - _skillUp.height) / 2 + 8;
			_skillUp.addEventListener(MouseEvent.MOUSE_MOVE,moveHandler);
			_skillDown.x = _skillUp.x;
			_skillDown.y =  (_headFream.height / 2 - _skillUp.height) / 2 + _headFream.height / 2 - 8; 
			_skillDown.addEventListener(MouseEvent.MOUSE_MOVE,moveHandler);
			_headFream.x = 14;
			_headFream.y = 14;
			_headFreamBigBg.addChild(_headFream);
			_headFream.addChild(_skillUp);
			_headFream.addChild(_skillDown);
		}
		private function moveHandler(e:MouseEvent):void
		{
			switch(e.currentTarget)
			{
				case _skillUp:
					UIManager.getToolTipManager().showTips(SimpleToolTips,e.target,"噬脑",ToolTipManager.UP);
					break;
				case _skillDown:
					UIManager.getToolTipManager().showTips(SimpleToolTips,e.target,"噬魂",ToolTipManager.DOWN);
					break;
			}
		}
		private function 	setHeroNameLable():void
		{
			_heroNameLabel.x = _headFream.x;
			_heroNameLabel.y = _headFream.y +_headFream.height + 5; 
			_heroNameLabel.text = "吉昌";
			_headFreamBigBg.addChild(_heroNameLabel);
		}
		private function setHeroPropertyLabel():void
		{
			_heroPropertyLabel.x = _heroNameLabel.x;
			_heroPropertyLabel.y = _heroNameLabel.y + 5 + _heroNameLabel.height;
			_heroPropertyLabel.text = "属性  :";
			_headFreamBigBg.addChild(_heroPropertyLabel);
		}
		private function setFiveLinesLabel():void
		{
			_fiveLinesLabel.x = _heroPropertyLabel.x + _heroPropertyLabel.width + 2;
			_fiveLinesLabel.y = _heroPropertyLabel.y;
			_fiveLinesLabel.text = "金";
			_headFreamBigBg.addChild(_fiveLinesLabel);
		}
		private var _heroInfo:Object;
		public function changInfoData(obj:Object):void
		{
			if(_style == 0)
			{
				_heroNameLabel.text = obj.name;
			}
			if(_style == 1)
			{
				_branchesLabel.text = obj.armsName;
				var arr:Array = ConfigAnalyze.analyze("skill");
				for(var i:uint = 0; i< ConfigAnalyze.analyze("skill").length;i++)
				{
					if(obj.skillList[0] == "")
					{
						_skillNameLabel.text = "";
						break;
					}
					if((arr[i] as SkillConfigObj).serverId == obj.skillList[0])
					{
						_skillNameLabel.text = (arr[i] as SkillConfigObj).serverId;
					}
				}
			}
		}
		public function refleshData(obj:Object):void
		{
			troopNum = obj.troopNum;
			_textInTo.text = troopNum.toString();
			
/*			_skillNameLabel.text = obj.skill;
			_branchesLabel.text = obj.armsName;*/
		}
	}
}